package com.zegome.game.gomestory.main.scene.vs;

import java.util.Random;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenPaths;
import aurelienribon.tweenengine.equations.Quad;
import aurelienribon.tweenengine.equations.Sine;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.zegome.facebook.IFacebookBridge;
import com.zegome.game.gomestory.main.D;
import com.zegome.game.gomestory.main.helper.IMAction;
import com.zegome.game.gomestory.main.helper.LoadingActor;
import com.zegome.game.gomestory.main.helper.TextDrawable;
import com.zegome.game.gomestory.main.scene.BaseGameScreen;
import com.zegome.game.main.AndroidGameController;
import com.zegome.game.util.helper.WidgetAccessor;

public class VSScreen extends BaseGameScreen {
	
	private Image bg;
	private ImageButton top_user;
	private ImageButton find;
	private ImageButton bet;
	private LoadingActor search;
	private float timeSearch = -1;	
	private Image V,S;
	
	private Image player1, player2;
	private Image boder1, boder2; 	
	private Table nameUser, infoUser, nameBoss, infoBoss;	
		
	public Image betMoney;
	public Label betText;
	
	private Label timeCountDown;
	private float countTime;
	private int time;
	private boolean isCountTime;	
	private Label myMoney, myMoneyOl;
	
	
	public VSScreen(AndroidGameController controller, String name, final IFacebookBridge bridge) {
		super(controller, name,bridge);		
		setConfig(true, false, true,true);		
	}

	public void show() {
	
		ctr.a.soundOnOff();
		
		if(ctr.d.typeBet == 0){
		
		ctr.d.typeBet = 1;
		ctr.d.betNum = 500;

		}
		
		time = 0;
		countTime = 0;
		isCountTime = false;
		
		ctr.a.isHasDialog.removeAllElements();
		ctr.d.backLis.removeAllElements();
		ctr.d.nameB = "";
		
		state = 0;
		stage.clear();
		createUi();		
		ctr.a.bgSong.play();
		
		createUser();
		
		if(ctr.d.passScreen[D.VS] == false){
			ctr.d.passScreen[D.VS] = true;
			ctr.d.saveAccount();
		}
		super.show();
	}
	
	public void createUi() {
		if (bg == null) {
			bg = ctr.stageHelper.createImage(ctr.a.bgVSRegion, 0, 0, (800), (480));
		}
		
		//addIconGomeSmallStage();
				
		if (find == null) {
			find = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.vsNorRegion,
					ctr.a.vsPressRegion, null, (550), (90));

			find.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {					
					if(ctr.a.isHasDialog() == true)
						return;
					if(ctr.d.typeBet == 1){
						if(ctr.d.moneyOnline<ctr.d.betNum){
							ctr.androidHelper.showToast(ctr.lang.getNotEnoughMoney(0, -1));
							ctr.stageHelper.createDialogMoney(VSScreen.this, stage, tweenManager);
							return;
						}						
					}
					if(ctr.d.typeBet == 2){
						if(ctr.d.money<ctr.d.betNum){
							ctr.androidHelper.showToast(ctr.lang.getNotEnoughMoney(1, -1));
							ctr.stageHelper.createDialogMoney(VSScreen.this, stage, tweenManager);
							return;
						}						
					}
					if(ctr.d.isHasAccount){						
						timeSearch = 3 + new Random().nextInt(4);
						search.setVisible(true);
						find.setVisible(false);
						ctr.androidHelper.sendGetBoss(VSScreen.this);
					}				
				}
			});
		}
		
		if (top_user == null) {
			
			top_user = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.topUser,
					null, null, (0), (480-50)-20,80,80);

			top_user.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {					
					if(ctr.a.isHasDialog() == true)
						return;
					if(search!=null && search.isVisible() == true){						
						return;
					}
					ctr.androidHelper.sendTopUser(VSScreen.this, stage, tweenManager);
				}
			});
			top_user.setColor(1f,1f,1f,0.5f);
			Tween tween = Tween.to(top_user, WidgetAccessor.OPACITY, 0.5f)
					.target(1f)
					.repeatYoyo(-1, 0.1f);		
					tween.start(tweenManager);
		}
		
		if (bet == null) {
			bet = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.betButton,
					ctr.a.betButton, null, (800-66), (0),66,66);

			bet.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {					
					if(ctr.a.isHasDialog() == true || infoBoss!=null )
						return;
					if(search!=null && search.isVisible() == true){
						return;
					}
					find.setVisible(false);
					ctr.stageHelper.createDialogBet(VSScreen.this, stage, tweenManager,new IMAction() {
						
						@Override
						public void action() {
							// TODO Auto-generated method stub							
							find.setVisible(true);
						}
					});
				}
			});
		}
		
		search = new LoadingActor(new TextDrawable(ctr.g.mFont));
		search.setPosition((600), (480-247));
		stage.addActor(bg);
		addIconGomeSmallStage();
		stage.addActor(search);
		
		search.setVisible(false);
		stage.addActor(find);
		stage.addActor(top_user);
		stage.addActor(bet);
		V = ctr.stageHelper.createImage(ctr.a.vRegion, (310), (160));
		S = ctr.stageHelper.createImage(ctr.a.sRegion, (310), (160));
		stage.addActor(V);
		stage.addActor(S);
		
		if(betText == null){
			betText = ctr.stageHelper.createLabel(ctr.g.mFontCombo, "500", Color.WHITE, 315+70, 480-380);
		}
		if(betMoney == null){
			betMoney = ctr.stageHelper.createImage(ctr.a.iconBigGomeMoney,315,480-380,70,70);
		}
		
		setBetUi();
		
		if(timeCountDown == null){
			timeCountDown = ctr.stageHelper.createLabel(ctr.g.mFontCombo, "3", Color.WHITE, 376, 480-134);
		}
		stage.addActor(betText);
		stage.addActor(betMoney);
		stage.addActor(timeCountDown);
		timeCountDown.setText("");
		
		find.setPosition((306), (-20));
		Tween.to(find, WidgetAccessor.POS_XY, 0.5f).target((306), (20)).ease(Sine.OUT).start(tweenManager);			
		S.setVisible(false);
		
		if(ctr.d.isHasAccount){
		//	createUser();
		}
		else{
			find.setVisible(false);
			top_user.setVisible(false);
			bet.setVisible(false);
			V.setVisible(false);
			S.setVisible(false);				
			top_user.setVisible(false);
		}
		
		
	}
	public void createUser(){
		if(infoUser!=null)
			infoUser.remove();
		if(nameUser!=null)
			nameUser.remove();
		
		infoUser = createInfo(false,(-50), (480-436),10);
		
		nameUser = createName(false, (42), (480),1f);
		
		stage.addActor(infoUser);
		stage.addActor(nameUser);
				
		Tween.to(infoUser, WidgetAccessor.POS_XY, 0.5f).target((30), (480-436)).ease(Sine.OUT).start(tweenManager);
		Tween.to(nameUser, WidgetAccessor.POS_XY, 0.5f).target((42), (480-170)).ease(Sine.OUT).start(tweenManager);
			
		if(ctr.d.isFirstInVS){
			ctr.stageHelper.createDialogBet(VSScreen.this, stage, tweenManager, new IMAction() {				
				@Override
				public void action() {
					// TODO Auto-generated method stub
					find.setVisible(true);
				}
			});
			find.setVisible(false);
			ctr.d.isFirstInVS = false;
		}
	}

	public void createBoss(){
		
		if(infoBoss!=null)
			infoBoss.remove();
		if(nameBoss!=null)
			nameBoss.remove();
		infoBoss = createInfo(true,(480+50), (480-436),2);
		nameBoss = createName(true, (520), (480),1f);
		
		//stage.addActor(S);
		stage.addActor(infoBoss);
		stage.addActor(nameBoss);
	
		S.setVisible(false);

		Tween.to(infoBoss, WidgetAccessor.POS_XY, 0.5f).target((511), (480-436)).ease(Sine.OUT).start(tweenManager);
		Tween.to(nameBoss, WidgetAccessor.POS_XY, 0.5f).target((520), (480-170)).ease(Sine.OUT)
		.setCallback(new TweenCallback() {			
			@Override
			public void onEvent(int arg0, BaseTween<?> arg1) {
				// TODO Auto-generated method stub				
				ctr.a.bgSong.pause();
				ctr.a.kiem_chem.play(ctr.a.vls);
				find.setVisible(false);
				S.setVisible(true);
				S.setPosition(400, 160);
				Tween.to(S, WidgetAccessor.POS_XY, 0.5f).target((310), (160)).ease(Sine.OUT)
				.setCallback(new TweenCallback() {					
					@Override
					public void onEvent(int arg0, BaseTween<?> arg1) {
						// TODO Auto-generated method stub
						time = 3;
						countTime = 0f;
						isCountTime = true;
						timeCountDown.setText("3");

					}
				})
				.start(tweenManager);			
			}
		})
		.start(tweenManager);
	}
	
	public Table createInfo(boolean isBoss, float x, float y, int level){

		Table table = null;
		table = ctr.stageHelper.createTableWithTransBg(ctr.g.mSkin, 0, 0, (265), (342));
		table.addActor(ctr.stageHelper.createImage(ctr.a.frameInfoRegion, 0, 0));
		Label levelStr;
		levelStr = ctr.stageHelper.createLabel(ctr.g.mFont, "", Color.WHITE, 0, 0);
		levelStr.setText(level+"");
		levelStr.setPosition((166), (230));
		
		Label info;
		info = ctr.stageHelper.createLabel(ctr.g.mFont, "", Color.WHITE, 0, 0);
		if(isBoss)
			info.setText(ctr.lang.total_win+": "+ctr.d.total_winB+ "\n"+ctr.lang.total_lose+": "+ctr.d.total_loseB);
		else{
			info.setText(ctr.lang.total_win+": "+ctr.d.total_win+ "\n"+ctr.lang.total_lose+": "+ctr.d.total_lose);
		}
		
		info.setPosition((20), (130-20));
		//table.addActor(levelStr);
		table.addActor(info);		
		if(isBoss == false)
		ctr.stageHelper.starGen(table, 5, 435-255-20, ctr.d.star, ctr.d.getTypeAchievement(), 2, true, ctr.d.achievement, ctr.d.exp);
		else{
			ctr.stageHelper.starGen(table, 5, 435-255-20, ctr.d.starB, ctr.d.getTypeAchievement(ctr.d.achievementB), 2, true, ctr.d.achievementB, ctr.d.expB);
		}
		table.setPosition(x, y);
		return table;
	}
	
	public void onManagedUpdate(float pDeltaTime){
				
		update(pDeltaTime);
		if(myMoney!=null && myMoneyOl!=null){
			myMoney.setText(" "+ctr.d.money);
			myMoneyOl.setText(" "+ctr.d.moneyOnline);
		}
		if(boder1!=null & player1!=null){
		boder1.setPosition(player1.getX()-4*player1.getScaleX(), player1.getY()-4*player1.getScaleX());
		boder1.setScale(player1.getScaleX());				
		}
		if(boder2!=null && player2!=null){
		boder2.setPosition(player2.getX()-4*player2.getScaleX(), player2.getY()-4*player2.getScaleX());
		boder2.setScale(player2.getScaleX());
		}
		if(search!=null && search.isVisible() == true){
			if(ctr.d.nameB.equals("")){								
			}
			else{
				//ctr.d.speedB = ctr.d.getTypeAchievement(ctr.d.)
				createBoss();
				search.setVisible(false);
				if(ctr.d.typeBet == 1){
					ctr.d.moneyOnline -= ctr.d.betNum;					
				}
				ctr.d.saveAccount();
			}			
//		if(timeSearch<0){			
//			createBoss();
//			search.setVisible(false);
//			//stage.addActor(play);
//			//play.setPosition((306), (-20));
//		}
//		else{
//			timeSearch -= pDeltaTime;
//		}		
		}
		
		if(isCountTime){
			if(time>0){						
				if(countTime>=1f){
					countTime = 0f;
					time --;
					timeCountDown.setText(time+"");
				}
				else{
					countTime+=pDeltaTime;
				}
			}		
			else{
				isCountTime = false;
				//
				player1.setPosition((20), (10));
				Tween.to(player1, WidgetAccessor.POS_XY, 2.0f)
				.waypoint((-50), (-50))
				.waypoint((300), (-200))
				.target((60), (-200))
				.ease(Quad.INOUT)
				.path(TweenPaths.catmullRom)
				.start(tweenManager);
				
				Tween.to(player1, WidgetAccessor.SCALE_XY, 2.0f)
				.target(3f,3f)
				.start(tweenManager);
				player2.setPosition((20), (10));
				Tween.to(player2, WidgetAccessor.POS_XY, 2.0f)
				.waypoint((250), (-50))
				.waypoint((-200), (-200))
				.target((-20), (-200))
				.ease(Quad.INOUT)
				.path(TweenPaths.catmullRom)
				.setCallback(new TweenCallback() {
					
					@Override
					public void onEvent(int arg0, BaseTween<?> arg1) {
						// TODO Auto-generated method stub								
						((VS2Screen)(getController().getScreen("VS2Screen"))).randLevel(false);
						gotoScreen(
								new IMAction() {
									
									@Override
									public void action() {
										// TODO Auto-generated method stub			
											ctr.a.loadRSGame(VS2Screen.LEVEL, true);																	
									}
								},
								new IMAction() {
									
									@Override
									public void action() {
										// TODO Auto-generated method stub
										ctr.a.loadedGame();
										ctr.a.loadedMusic2();																	
										ctr.a.loadedRSGame(VS2Screen.LEVEL, true);
									}
								},
							    new IMAction() {
									
									@Override
									public void action() {
										// TODO Auto-generated method stub
										fadeOut(new TweenCallback() {
											@Override
											public void onEvent(int type, BaseTween<?> source) {
												ctr.a.yesSound.play(ctr.a.vls);
												ctr.a.bgSong.pause();
												ctr.a.clearVS();
												clear();
												mController.setScreen(mController
														.getScreen("VS2Screen"));
											}
										});	
									}
								});
					}
				})
				.start(tweenManager);
				
				Tween.to(player2, WidgetAccessor.SCALE_XY, 2.0f)
				.target(3f,3f)						
				.start(tweenManager);
	
				//
			}
		}
				
		super.onManagedUpdate(pDeltaTime);
	}
	public void backKeyProcess(){
		if(false){
			if(ctr.a.isHasDialog())
				return;
		final ChangeListener okLis = new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {							
				fadeOut(new TweenCallback() {					
					@Override
					public void onEvent(int arg0, BaseTween<?> arg1) {
						// TODO Auto-generated method stub
						ctr.a.bgSong.pause();
						ctr.a.clearVS();
						clear();
						ctr.a.loadedMenu();
						mController.setScreen(mController.getScreen("MenuScreen"));											
					}
				});
			}
		};
		//postRunnable(new Runnable() {
			
		//	@Override
		//	public void run() {
				// TODO Auto-generated method stub
				ctr.stageHelper.createDialog(stage, tweenManager,ctr.lang.backMenuConfirm, 2, okLis, null);
		//	}
		//});
		}
		else{
			if(ctr.a.isHasDialog())
				return;
			
			if(infoBoss!=null)
				return;
			
			if(search!=null && search.isVisible() == true){
				ctr.androidHelper.showToast(ctr.lang.searchingPlayer);
				return;
			}
		//	postRunnable(new Runnable() {
				
		//		@Override
		//		public void run() {
					// TODO Auto-generated method stub
			if(ctr.showMobileCore(new IMAction() {
				
				@Override
				public void action() {
					// TODO Auto-generated method stub
					postRunnable(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							ctr.a.bgSong.pause();
							ctr.a.clearVS();
							clear();
							ctr.a.loadedMenu();
							mController.setScreen(mController.getScreen("MenuScreen"));		
						}
					});					
				}
			}))
			{
				return;
			}
					ctr.a.bgSong.pause();
					ctr.a.clearVS();
					clear();
					ctr.a.loadedMenu();
					mController.setScreen(mController.getScreen("MenuScreen"));		
		//		}
		//	});									
		}
	}
		
	public Table createName(boolean isEnemy, float x, float y, float scale){		
		Table table = null;		
		Label moneyStr, nameStr;
		
		table = ctr.stageHelper.createTableWithTransBg(ctr.g.mSkin, 0, 0, (244*scale), (99*scale));
		if(isEnemy){
			table.addActor(ctr.stageHelper.createImage(ctr.a.avatarFrame, 0, 0,(244*scale), (99*scale)));
			player2 = ctr.stageHelper.createImage(ctr.a.avatarBoss, 10, 13,(72*scale), (72*scale));		
			boder2 = ctr.stageHelper.createImage(ctr.a.boderAvatar, 6, 9, (80*scale), (80*scale));
			table.addActor(player2);
			table.addActor(boder2);
			//table.addActor(ctr.stageHelper.createImage(ctr.a.fbAttackIcon, (70), (10),(28*scale), (28*scale)));
			moneyStr = ctr.stageHelper.createLabel(ctr.g.mFont, "", Color.WHITE, 0, 0);
			moneyStr.setText(" "+ctr.d.gomeB);
			moneyStr.setPosition((110*scale), (47*scale));

			nameStr = ctr.stageHelper.createLabel(ctr.g.mFont, "", Color.WHITE, 0, 0);
			if(ctr.d.nameB.length()>10){
				nameStr.setText(ctr.d.nameB.substring(0, 5)+"..."+ctr.d.nameB.substring(ctr.d.nameB.length()-2, ctr.d.nameB.length()));
			}
			else{
			nameStr.setText(ctr.d.nameB);
			}
			nameStr.setPosition((87*scale), (77*scale));
			
			table.addActor(ctr.stageHelper.createImage(ctr.a.iconGomeMoney, (87), (27),(38), (41)));
			
		}
		else{			
			table.addActor(ctr.stageHelper.createImage(ctr.a.avatarFrame, 0, 0,(244*scale), (99*scale)));
			
			table.addActor(ctr.stageHelper.createImage(ctr.a.iconGomeMoney, (87), (57),(38), (41)));
			table.addActor(((ctr.stageHelper.createImage(ctr.a.onlineGomeMoney, (87), (35),(30), (30)))));
			
			player1 = ctr.stageHelper.createImage(ctr.a.avatarMe, (10), (13),(72*scale), (72*scale));
			table.addActor(player1);
			boder1 = ctr.stageHelper.createImage(ctr.a.boderAvatar, 6, 9, (80*scale), (80*scale));
			table.addActor(boder1);

			
			
			moneyStr = ctr.stageHelper.createLabel(ctr.g.mFont, "", Color.WHITE, 0, 0);
			moneyStr.setText(" "+ctr.d.money+"");
			moneyStr.setPosition((110*scale), (47*scale));

			nameStr = ctr.stageHelper.createLabel(ctr.g.mFont, "", Color.WHITE, 0, 0);
			nameStr.setText(" "+ctr.d.moneyOnline);
			nameStr.setPosition( (110*scale), (77*scale));
			
			myMoney = moneyStr;
			myMoneyOl = nameStr;
			
			//table.addActor(ctr.stageHelper.createImage(ctr.a.iconGomeMoney, (102), (28),(38), (41)));
			
		}
		table.addActor(moneyStr);
		table.addActor(nameStr);
		table.setPosition(x, y);
		return table;
	}
	
	public void setBetUi(){

//		if(betText == null){
//			betText = ctr.stageHelper.createLabel(ctr.g.mFont, "Cược 200", Color.WHITE, 320, 480-125);
//		}
//		if(betMoney == null){
//			betMoney = ctr.stageHelper.createImage(ctr.a.iconGomeMoney,332+betText.getTextBounds().width,480-125-10);
//		}
				
		if(ctr.d.typeBet == 1)
		betMoney.setDrawable(new TextureRegionDrawable(ctr.a.iconBigGomeMoney));
		else
			betMoney.setDrawable(new TextureRegionDrawable(ctr.a.bigDiamond));		
		betText.setText(ctr.d.betNum+"");		
	}
	
	public void clear(){
		super.clear();
		bg = null;
		find = null;
		top_user = null;
		search = null;
		timeSearch = -1;
		bet = null;
		betMoney = null;
		betText = null;
		timeCountDown = null;
		V = null;
		S = null;
		player1 = null; player2 = null; 
		boder1 = null; boder2 = null;
		infoBoss = null;
		infoUser = null;
		nameBoss = null;
		nameUser = null;
		
	}
}
